<template>
    <div class="boxMain">
        <van-list v-model:loading="store.loading" :finished="store.finished" finished-text="没有更多了" @load="onLoad"
            :immediate-check="false" offset="5">
            
                <div class="main" v-for="item in store.mainDataList" :key="item.filmId">

                <div class="imgs" @click="onDetail(item.filmId)">
                    <img :src="item.poster" alt="" style="width:66px;height: 92px;">
                </div>
                <div class="spans" @click="onDetail(item.filmId)">
                    <span>{{ item.name }}</span> <span class="filmType">{{ item.filmType.name }}</span>
                    <br>
                    <span>观众评分 {{ item.grade }}</span>
                    <br>
                    主演:<span v-for="data in item.actors" style="margin-left:5px;">{{ data.name }}</span>
                    <br>
                    <span>{{ item.nation }}</span>
                    |
                    <span>{{ item.runtime }}分钟</span>
                </div>
                <div class="buttons">
                    <van-button plain hairline type="primary" size="mini" @click="toListCinemas(item.filmId)">购票</van-button>
                </div>

</div>

            
        </van-list>
    </div>
</template>

<script setup>
import { inject, onMounted, provide, ref } from 'vue';
import listStore from '../store';
import axios from 'axios';
import router from "../router/index"

const store = listStore()


const type = inject("type")


onMounted(() => {
    console.log("on");
    console.log(store.pageNum);
    store.total = 0
    store.pageNum = 1
    console.log("手动更改为1",store.pageNum);
    store.finished = false
    store.getList(store.cityId,store.pageNum, type.value)
    onLoad()
})

const onLoad = async() => {  
   
      if(store.total === store.mainDataList.length && store.total !== 0) {

         // 数据全部加载完成
         console.log("数据取完");
         console.log("到底了");
       store.finished = true
       store.pageNum = 1
        return
      }
    //   store.getList(store.pageNum++,type.value)
      store.pageNum++
        const res =  await axios({  
            url:`https://m.maizuo.com/gateway?cityId=${store.cityId}&pageNum=${store.pageNum}&pageSize=10&type=${type.value}&k=4231560`,
            headers:{
                'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.2.1","e":"16923257271850709997780993","bc":"110100"}',
                'X-Host':'mall.film-ticket.film.list'
            }
    })
    console.log(store.total,"....",store.mainDataList.length);
     
    store.mainDataList = [...store.mainDataList,...res.data.data.films]
     // 加载状态结束
     store.loading = false;
      
    };

// 点击电影详情页
const onDetail = ((filmId) =>{
    router.push({name:"film",params:{filmId}})
    store.bottom = false
})

// 点击购票按钮
const toListCinemas = ((filmId)=>{
    // 路由跳转购票
    router.push({name:"listCinemas",params:{filmId}})
})



</script>

<style scoped>
.boxMain {
    height: 100vh;
}

.main {
    margin-top: 20px;
    padding: 0 10px;
    position: relative;
    height: 92px;
}

.spans {
    position: absolute;
    color: #797d82;
    font-size: 13px;
    top: 5px;
    left: 20%;
    line-height: 20px;
    width: 250px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.spans :first-child {
    color: #191a1b;
    font-size: 16px;
}

.filmType {
    display: inline-block;
    width: 20px;
    height: 14px;
    line-height: 14px;
    background-color: #d2d6dc;
    text-align: center;
    color: #fff;
}

/* .line {
    display: inline-block;
    width: 2px;
    background-color: #797d82;
    height: 20px;
} */
.buttons {
    position: absolute;
    top: 35%;
    left: 85%;
}
</style>

